package com.han.neo4jjava.mysql.location;

import com.han.neo4jjava.core.location.Location;
import com.han.neo4jjava.core.location.LocationRepo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;

import static java.util.stream.Collectors.toList;

@Component
@RequiredArgsConstructor
public class MysqlLocationRepoImpl implements LocationRepo {

    private final LocationMapper locationMapper;

    @Override
    public List<Location> findAll() {
        List<LocationModel> models = locationMapper.selectList(null);
        return models.stream().map(this::convert2Location).collect(toList());
    }

    @Override
    public Location findById(Long id) {
        LocationModel model = locationMapper.selectById(id);
        return this.convert2Location(model);
    }

    @Override
    public void saveAll(List<Location> locations) {
        locations.stream().map(this::convert2LocationModel).forEach(locationMapper::insert);
    }

    @Override
    public Location save(Location location) {
        return null;
    }

    private Location convert2Location(LocationModel model) {
        Location location = new Location();
        location.setId(model.getId());
        location.setAddress(model.getAddress());
        return location;
    }

    private LocationModel convert2LocationModel(Location location) {
        LocationModel model = new LocationModel();
        model.setId(location.getId());
        model.setAddress(location.getAddress());
        return model;
    }
}
